package br.ufrj.dcc.tp.cliente.gui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

import br.ufrj.dcc.tp.cliente.controller.ClientController;
import br.ufrj.dcc.tp.cliente.gui.event.EventType;
import br.ufrj.dcc.tp.cliente.gui.event.MessageEvent;
import br.ufrj.dcc.tp.utils.Config;

/**
 * Configura as opcoes de conexao do cliente para que esse possa 
 * conectar-se ao servidor.
 */
public class Options extends JFrame{
	
	private static final long serialVersionUID = 1L;
	
	/** Label do ip do servidor. */
	private JLabel hostIPLabel = new JLabel("IP do Servidor");
	/** Label da porta do servidor. */
	private JLabel hostPortLabel = new JLabel("Porta do Servidor");
	/** IP do servidor. */
	private JTextField hostIP;
	/** Porta do servidor */
	private JTextField hostPort;
	/** Nome de usuario. */
	private JTextField nome;
	/** Label do nome de usuario. */
	private JLabel nomeLabel = new JLabel("Nome");
	
	/** Botao para realizar a conexao. */
	private JButton confirm = new JButton("Conectar");
	/** Controller do cliente. */
	private ClientController controller;
	
	public Options(ClientController controller) {
		super("Cliente");
		this.controller = controller;
		
		JPanel painel = new JPanel();
		JPanel painelNorth = new JPanel();
		JPanel painelSouth = new JPanel();
		
		Container c = getContentPane();
		
		painelNorth.setLayout(new GridLayout(3, 2));
		
		hostIP = new JTextField();
		hostPort = new JTextField();
		nome = new JTextField();
		
		nome.addActionListener(controller);
		hostIP.addActionListener(controller);
		hostPort.addActionListener(controller);
		
		hostIP.setText(Config.DEFAULT_SERVER_HOSTNAME.toString());
		hostPort.setText(Config.DEFAULT_SERVER_PORT.toInteger().toString());
		
		painelNorth.add(hostIPLabel);
		painelNorth.add(hostIP);
		painelNorth.add(hostPortLabel);
		painelNorth.add(hostPort);
		painelNorth.add(nomeLabel);
		painelNorth.add(nome);
		painelSouth.add(confirm);
		
		painel.setLayout(new BorderLayout());
		painel.setBorder(new TitledBorder("Configurações"));
		
		painel.add(painelNorth, BorderLayout.NORTH);
		painel.add(painelSouth, BorderLayout.SOUTH);
		
		c.add(painel);
		
		confirm.addActionListener(controller);
		
		//window parameters
		setSize(350, 160);
		setResizable(false);
		setMinimumSize(this.getSize());
		setLocationRelativeTo(null);
		setVisible(true);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}
	
	@Override
	public void dispose() {
		controller.finish();
		silentDispose();
	}
	
	public void silentDispose() {
		super.dispose();
	}
	
	public String getNome() {
		return nome.getText();
	}
	
	public void setNome(String nome) {
		try {
			SwingUtilities.invokeAndWait(new MessageEvent(this.nome, nome, EventType.SET_TEXT));
		} catch (Exception e) {}
	}
	
	public String getHostIP() {
		return hostIP.getText();
	}

	public void setHostIP(String hostIP) {
		try {
			SwingUtilities.invokeAndWait(new MessageEvent(this.hostIP, hostIP, EventType.SET_TEXT));
		} catch (Exception e) {
		}
	}

	public Integer getHostPort() {
		try {
			return Integer.parseInt(hostPort.getText());
		} catch( NumberFormatException e) {
			return null;
		}
	}

	public void setHostPort(int hostPort) {
		try {
			SwingUtilities.invokeAndWait(new MessageEvent(this.hostPort, Integer.toString(hostPort), EventType.SET_TEXT));
		} catch (Exception e) {
		}
	}
	
	public JButton getConfirm() {
		return confirm;
	}
	
	public boolean isYours(Object e) {
		return e == hostPort || e == hostIP || e == nome || e == confirm;
	}
}
